package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.ui.activity.ChangeTimeActivity;
import com.cljz.makepassenger.utils.date.GetSystemTimeUtil;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/4/11.
 */

public class ChangeTimeP extends BaseP<ChangeTimeActivity> {
    public void getInviteUpdate(String id, String time) {
        try {
            HashMap<String, String> paramsMap = new HashMap<>();
            paramsMap.put("token", getToken());
            paramsMap.put("project_id", id);
            paramsMap.put("project_time", String.valueOf(GetSystemTimeUtil.dateToStamp(time)));
            String body = Base64Utils.enCode(gson.toJson(paramsMap));
            final RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
            Api.getResponseInfoAPI().getInviteUpdateData(requestBody)
                    .compose(XApi.<ResponseBean>getApiTransformer())         //异常处理
                    .compose(XApi.<ResponseBean>getScheduler())              //线程切换
                    .compose(getV().<ResponseBean>bindToLifecycle())
                    .subscribe(new ApiSubcriber<ResponseBean>() {
                        @Override
                        protected void onFail(NetError error) {
                        }

                        @Override
                        public void onNext(ResponseBean responseBean) {
                            if ("0".equals(responseBean.getError_code())) {
                                getV().inviteUpdateSucceed();
                            } else if ("1000".equals(responseBean.getError_code())) {
                                getV().loginOvertime(1);
                            } else {
                                getV().inviteUpdateFail(responseBean.getMsg());
                            }
                        }
                    });
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
